/**
 * @description: 重写ctx.success和ctx.fail方法，返回json数据
 * @author: 王琛
 * @Date: 2023-09-11 11:53:10
 * @param {*}
 * @return {*}
 */
function routerResponse(option = {}) {
	return async function (ctx, next) {
		ctx.success = function (data) {
			ctx.type = option.type || 'json'
			ctx.body = {
				code: option.successCode || 200,
				msg: option.successMsg || 'success',
				data: data
			}
		}
		
		ctx.fail = function (msg, code) {
			ctx.type = option.type || 'json'
			ctx.body = {
				code: code || option.failCode || 99,
				msg: msg || option.successMsg || 'fail',
			}
		}
		
		await next()
	}
	
}

module.exports = routerResponse;
